<?php
function GetHttpVirusNumber()
{
	$command = "cat /usr/local/apache2/htdocs/http.number";
	$http = shell_exec($command);
	return $http;
}

function GetMailVirusNumber()
{
	$command = "cat /usr/local/apache2/htdocs/pop3.number";
	$pop3 = shell_exec($command);
	$command = "cat /usr/local/apache2/htdocs/smtp.number";
	$smtp = shell_exec($command);
	$ret = $pop3 + $smtp;
	return $ret;
}

function GetFtpVirusNumber()
{
	$command = "cat /usr/local/apache2/htdocs/ftp.number";
	$ftp = shell_exec($command);
	return $ftp;
}

function QueryHttpVirus()
{

	$command = "cat /var/log/log.txt | grep HTTP > http.txt ";
	shell_exec($command);

	$handle = @fopen("http.txt", "r");
	$list = array();
	$ret_list = array();
	if (!$handle) 
	{
		return ;
	}
	while (!feof($handle)) 
	{
   		$list[] = fgets($handle, 4096);
   	}
      	fclose($handle);

	$i = 0;
	$k = 0;
	foreach($list as $l)
	{
		$str_list = explode(" ", $l);
		for ($k = 0; $k < count($str_list); $k ++)
		{
			if ($str_list[$k] == 200)
			{
				$ret_list[$i]['Src'] = $str_list[ $k - 2];
				$ret_list[$i]['Dsc'] = $str_list[ $k + 1];
				$ret_list[$i]['Name'] = $str_list[ $k + 5];
				$i ++;
			}
		}
	}
	
	/*foreach ($ret_list as $r)
	{
		echo $r['Src']."\n". $r['Dsc']. "\n". $r["Name"]. "\n";
	}*/
	return $ret_list;
}

function QueryRunLog()
{
	$handle = @fopen("/ldisk/system/boot.log", "r");
	$list = array();
	$ret_list = array();
	if (!$handle) 
	{
		return ;
	}
	while (!feof($handle)) 
	{
   		$list[] = fgets($handle, 4096);
   	}
      	fclose($handle);
	$i = 0;
	$k = 0;
	foreach($list as $l)
	{
		$str_list = explode(" ", $l);
		
		$ret_list[$i]['Act'] = $str_list[0];
		$ret_list[$i]['Time'] = $str_list[1]. " ". $str_list[2]. " ". $str_list[3]. " ".
					$str_list[4]. " ". $str_list[5]. " ". $str_list[6];
			$i ++;
	}
	return $ret_list;
	
}
function QueryMailVirus()
{

	$command = "cat /var/log/log.txt | grep SMTP | grep virus: | sed 's/<//g' | sed 's/>//g' > smtp.txt";
	shell_exec($command);

	$handle = @fopen("smtp.txt", "r");
	$list = array();
	$ret_list = array();
	if (!$handle) 
	{
		return ;
	}
	while (!feof($handle)) 
	{
   		$list[] = fgets($handle, 4096);
   	}
      	fclose($handle);

	$i = 0;
	$k = 0;
	$n = 0;
	$j = 0;
	foreach($list as $l)
	{
		$str_list = explode(" ", $l);
		$j = 0;
		for ($k = 0; $k < count($str_list); $k ++)
		{
			//print $k. "=". $str_list[$k]. "\n";
			if ($str_list[$k] == "from" && !$j )
			{
				$ret_list[$i]['Src'] = $str_list[ $k + 1];
				$ret_list[$i]['Dsc'] = $str_list[ $k + 3];
				$n ++;
				$j ++;
			}
			if ($str_list[$k] == "user:")
			{
				$ret_list[$i]['SrcMail'] = $str_list[ $k - 3];
				$ret_list[$i]['DscMail'] = $str_list[ $k - 1];
				$n ++;
			}
			if ($str_list[$k] == "virus:")
			{
				$ret_list[$i]['Name'] = $str_list[ $k + 1];
				$n ++;
			}
			if ($n == 3)
				$i ++;
		}
	}
	
	/*foreach ($ret_list as $r)
	{
		echo $r['Src']."\n". $r['Dsc']. "\n". $r["Name"]. "\n". $r['SrcMail']. "\n". $r['DscMail']."\n";
	}*/
	return $ret_list;
}
function QueryMailPop3Virus()
{

	$command = "cat /var/log/log.txt | grep POP3 | grep virus: | sed 's/<//g' | sed 's/>//g' > pop3.txt";
	shell_exec($command);

	$handle = @fopen("pop3.txt", "r");
	$list = array();
	$ret_list = array();
	if (!$handle) 
	{
		return ;
	}
	while (!feof($handle)) 
	{
   		$list[] = fgets($handle, 4096);
   	}
      	fclose($handle);

	$i = 0;
	$k = 0;
	$n = 0;
	$j = 0;
	foreach($list as $l)
	{
		$str_list = explode(" ", $l);
		$j = 0;
		for ($k = 0; $k < count($str_list); $k ++)
		{
			//print $k. "=". $str_list[$k]. "\n";
			if ($str_list[$k] == "from" && !$j )
			{
				$ret_list[$i]['Src'] = $str_list[ $k + 1];
				$ret_list[$i]['Dsc'] = $str_list[ $k + 3];
				$n ++;
				$j ++;
			}
			if ($str_list[$k] == "user:")
			{
				$ret_list[$i]['SrcMail'] = $str_list[ $k - 3];
				$ret_list[$i]['DscMail'] = $str_list[ $k - 1];
				$n ++;
			}
			if ($str_list[$k] == "virus:")
			{
				$ret_list[$i]['Name'] = $str_list[ $k + 1];
				$n ++;
			}
			if ($n == 3)
				$i ++;
		}
	}
	
	/*foreach ($ret_list as $r)
	{
		echo $r['Src']."\n". $r['Dsc']. "\n". $r["Name"]. "\n". $r['SrcMail']. "\n". $r['DscMail']."\n";
	}*/
	return $ret_list;
}
function QueryFtpVirus()
{

	$command = "cat /var/log/log.txt | grep VIRUS_INFECTED > ftp.txt";
	shell_exec($command);

	$handle = @fopen("ftp.txt", "r");
	$list = array();
	$ret_list = array();
	if (!$handle) 
	{
		return ;
	}
	while (!feof($handle)) 
	{
   		$list[] = fgets($handle, 4096);
   	}
      	fclose($handle);

	$i = 0;
	$k = 0;
	foreach($list as $l)
	{
		$str_list = explode(" ", $l);
		$j = 0;
		for ($k = 0; $k < count($str_list); $k ++)
		{
			//print $k. "=". $str_list[$k]. "\n";
			if ($str_list[$k] == "DOWNLOADED" )
			{
				$ret_list[$i]['Src'] = $str_list[ $k - 1];
				$ret_list[$i]['Dsc'] = $str_list[ $k + 1];
			}
			$i ++;
		}
	}
	
	/*foreach ($ret_list as $r)
	{
		echo $r['Src']."\n". $r['Dsc']. "\n". $r["Name"]. "\n". $r['SrcMail']. "\n". $r['DscMail']."\n";
	}*/
	return $ret_list;
}
?>
